package com.hm.hall.user.controller;


import com.hm.hall.commons.constans.ErrorCode;
import com.hm.hall.commons.constans.po.StubInfo;
import com.hm.hall.commons.domain.resp.BaseResponse;
import com.hm.hall.user.controller.req.EmailLoginReq;
import com.hm.hall.user.controller.req.EmailRegReq;
import com.hm.hall.user.controller.req.ThirdLoginReq;
import com.hm.hall.user.controller.resp.LoginResp;
import com.hm.hall.user.service.UserLoginService;
import com.hm.hall.user.service.UserService;
import com.hm.hall.commons.constans.ValueConstants;
import com.hm.hall.commons.util.Validators;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用戶登陸
 *
 * @author Luhong
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserLoginService userLoginService;

    @Autowired
    private UserService userService;


    /**
     * 使用第三方凭证登录
     * <p>
     * <p>
     * (必须) | 微信 unionid
     *
     * @param unionid;  (必须) | 第三方登录唯一凭证
     * @param username; (必须) | 第三方接入商渠道标示 机器人 robot 游客 hoobic.vi 微信 hoomic.weixin.mp
     *                  邮箱hoobic.email QQ hoobic.qq.qc
     * @param channel;  (必须) | 当前时间戳
     * @param time;     (可传) | 用户昵称
     * @param nickname; (可传) | 用户头像链接
     * @param avatar;   (可传) | 性别 0:未设置,1:男,2:女
     * @param sex;
     * @return { "errMsg": "", "errorCode": 0, "failureTime_L": 1542851984169,
     * "lastGetLoginRespTime": 0, "sessionToken":
     * "8123fb1283f4dbeb04d145e604ed18a1", "sysTime": 1540259984164,
     * "timestamp_L": 1540259984169, "user": { "avatar":
     * "http://pub.froup.net/hoomic/images/game/hall/hoobic_app_default_avatar1.png",
     * "nickname": "跳跳", "userId": 88000006 } }
     */
    @RequestMapping("/third_login")
    @ResponseBody
    public LoginResp userThirdLogin(ThirdLoginReq req) {
        if (req.sex == null) {
            req.sex = 2;
        }
        return userLoginService.thirdLogin(req);
    }

    /**
     * 使用邮箱登录游戏
     * <p>
     * (必须) | 邮箱
     *
     * @param String email;
     *               <p>
     *               (必须) | 密码
     * @param String password;
     *               <p>
     *               (必须) | 当前时间戳
     * @param long   time;
     *               <p>
     *               (可传) | 用户昵称
     * @param String nickname;
     *               <p>
     *               (可传) | 用户头像链接
     * @param String avatar;
     *               <p>
     *               (可传) | 性别 0:未设置,1:男,2:女
     * @param int    sex;
     * @return { "errMsg": "", "errorCode": 0, "failureTime_L": 1542851984169,
     * "lastGetLoginRespTime": 0, "sessionToken":
     * "8123fb1283f4dbeb04d145e604ed18a1", "sysTime": 1540259984164,
     * "timestamp_L": 1540259984169, "user": { "avatar":
     * "http://pub.froup.net/hoomic/images/game/hall/hoobic_app_default_avatar1.png",
     * "nickname": "跳跳", "userId": 88000006 } }
     */
    @RequestMapping("/email_login")
    @ResponseBody
    public LoginResp userEmailLogin(EmailLoginReq req) {
        return userLoginService.emailLogin(req);
    }

    /**
     * 使用邮箱登录游戏
     *
     * @param (必须)    | 邮箱
     * @param String  email;
     *                <p>
     *                (必须) | 密码
     * @param String  password;
     *                <p>
     *                (必须) | 重复密码
     * @param String  passwordRe;
     *                <p>
     *                (必须) | 当前时间戳
     * @param long    time;
     *                <p>
     *                (可传) | 用户昵称
     * @param String  nickname;
     *                <p>
     *                (可传) | 用户头像链接
     * @param String  avatar;
     *                <p>
     *                (可传) | 性别 0:未设置,1:男,2:女
     * @param Integer sex;
     * @return { "errMsg": "", "errorCode": 0, "failureTime_L": 1542851984169,
     * "lastGetLoginRespTime": 0, "sessionToken":
     * "8123fb1283f4dbeb04d145e604ed18a1", "sysTime": 1540259984164,
     * "timestamp_L": 1540259984169, "user": { "avatar":
     * "http://pub.froup.net/hoomic/images/game/hall/hoobic_app_default_avatar1.png",
     * "nickname": "跳跳", "userId": 88000006 } }
     */
    @RequestMapping("/email_registration")
    @ResponseBody
    public LoginResp userEmailRegistration(EmailRegReq req) {
        if (null == req) {
            return new LoginResp(ErrorCode.param_missing);
        }
        if (null == req.sex || req.sex == ValueConstants.DEFAULT_ZERO) {
            req.sex = 2;
        }
        return userLoginService.emailRegistration(req);
    }

    /**
     * 修改用户信息 只有通过邮箱注册和游客可以修改 每7*24小时只能修改一次
     *
     * @param userId   用户ID
     * @param avatar   头像
     * @param nickname 昵称
     * @param sex      性别 0:未设置,1:男,2:女
     * @return errorCode 2000:"请您登录后重试" ; 1024:"当前渠道不能修改信息" ;1025, "您在七天内修改过用户信息"
     */
    @RequestMapping("/update_user_info")
    @ResponseBody
    public BaseResponse updateUserInfo(Long userId, String avatar, String nickname, Integer sex) {
        if (null == sex) {
            sex = 1;
        }
        return userService.updateUserInfo(userId, avatar, nickname, sex);
    }

    /**
     * 修改语言
     *
     * @param userId
     * @param lang   语言 zh-中文 ，en-英文
     * @return
     */
    @RequestMapping("/update_lang")
    @ResponseBody
    public BaseResponse updateUserLang(Long userId, String lang) {
        return userService.updateUserLang(userId, lang);
    }

    /**
     * @param email 邮箱
     * @return errorCode 1032: "邮箱格式正确" ; 2005: "当前用户不存在"
     */
    @RequestMapping("/send_find_password_code")
    @ResponseBody
    public BaseResponse sendFindPasswordCode(StubInfo stub, String email) {
        if (!Validators.checkEmail(email)) {
            return new BaseResponse(ErrorCode.email_incorrect_format);
        }
        return userService.sendVerificationCode(email, "");
    }

    /**
     * @param email 邮箱
     * @param code  验证码
     * @return errorCode 1026:"缺失参数";errorCode 1032: "邮箱格式不正确" ;1030, "验证码不匹配"
     */
    @RequestMapping("/check_find_password_code")
    @ResponseBody
    public BaseResponse checkFindPasswordCode(String email, String code) {
        if (StringUtils.isBlank(email) || StringUtils.isBlank(code)) {
            return new BaseResponse(ErrorCode.param_missing);
        }
        if (!Validators.checkEmail(email)) {
            return new BaseResponse(ErrorCode.email_incorrect_format);
        }
        return userService.checkVerificationCode(email, code);
    }

    /**
     * @param email      邮箱
     * @param password   密码
     * @param passwordRe 密码重复
     * @param code       验证码
     * @return 1027:"两次输入密码不一致"
     */
    @RequestMapping("/update_password")
    @ResponseBody
    public BaseResponse updatePassword(String email, String password, String passwordRe, String code) {
        if (StringUtils.isBlank(email) || StringUtils.isBlank(password) || StringUtils.isBlank(passwordRe)) {
            return new BaseResponse(ErrorCode.param_missing);
        }
        if (!Validators.checkEmail(email)) {
            return new BaseResponse(ErrorCode.email_incorrect_format);
        }
        return userService.updatePassword(email, password, passwordRe, code);
    }


    /**
     * /user/modify_password
     *
     * @param userId       用户ID
     * @param passwordNew  新密码
     * @param passwordNew2 新密码确认
     * @param passwordOld  旧密码
     *                     errorCode 1026:"缺失参数";errorCode 1027: "两次输入密码不一致" ;2005, "当前用户不存在";1033,"旧密码输入错误"
     * @return
     */
    @RequestMapping("/modify_password")
    @ResponseBody
    public BaseResponse modifyassword(long userId, String passwordNew, String passwordNewR, String passwordOld) {
        if (StringUtils.isBlank(passwordNew) || StringUtils.isBlank(passwordNewR) || StringUtils.isBlank(passwordOld)) {
            return new BaseResponse(ErrorCode.param_missing);
        }
        if (!passwordNew.equals(passwordNewR)) {
            return new BaseResponse(ErrorCode.not_equel_password);
        }
        return userService.modifyPassWord(userId, passwordOld, passwordNew);
    }

}
